package Background;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import javax.swing.JOptionPane;


/**
 * A class to manage the list of users, judges and designs.
 * 
 * @author Chris Petcher and Justin Wong
 * @version 2/01/2013
 * 
 */

public class Lists  {

	/**
	 * An arrayList that holds a list of contestants.
	 */
	
	private static ArrayList<Contestant> contestantList = new ArrayList<Contestant>();
	
	/**
	 * An arrayList that holds a list of judges.
	 */
	
	private static ArrayList<Judge> judgeList = new ArrayList<Judge>();
	
	/**
	 * An arrayList that holds the contest winners.
	 */
	
	private static ArrayList<Contestant> contestWinners = new ArrayList<Contestant>();
	

	/**
	 * A file to handle the contestant users of the program.
	 */
	
	private static File contestant_file;

	/**
	 * A file to handle the judge users of the program.
	 */
	
	private static File judge_file; 
	
	/**
	 * Adds a contestant to the entry list.
	 * @param contestant The contestant to add to the list
	 */

	public static void addContestant(final Contestant contestant){
		contestantList.add(contestant);
		
	}

	/**
	 * Adds a judge to the judge list.
	 * @param contestant The judge to add to the list.
	 */

	public static void addJudge(final Judge judge){
		judgeList.add(judge);
		
	}

	/**
	 * Adds a judge to the judge list.
	 * @param contestant The judge to add to the list.
	 */

	public static void addWinner(final Contestant winner){
		
		boolean valid = true;
		
		for (Contestant c : contestWinners)
		{
			if(c.getUserName().equals(winner.getUserName())){
				valid = false;
			}
		}
		
		if(valid){
			contestWinners.add(winner);
      	  	JOptionPane.showMessageDialog(null, "Contestant: " +  winner.getName() + 
      			  " has been added to the list of winners!");

		}
		else{
			JOptionPane.showMessageDialog(null, "This user has already been selected as a winner!");
		}
		
	}

	
	/**
	 * Gets the list of contestants.
	 * @return The list of all the contestants.
	 */
	
	public static ArrayList<Contestant> getContestantList(){
		return contestantList;
	}
	
	/**
	 * Gets the list of judges. 
	 * @return The list of all the judges.
	 */
	
	public static ArrayList<Judge> getJudgeList(){
		return judgeList;
	}

	/**
	 * Gets the list of winners.
	 * @return The list of all the contest winners.
	 */
	
	public static ArrayList<Contestant> getWinners(){
		return contestWinners;
	}

	public static void readData() {
		try {
			ObjectInputStream contestants = new ObjectInputStream(new FileInputStream("Output/contestant.dat"));
			Object readIn;
			while ((readIn = contestants.readObject()) != null) {
				Contestant contestant = (Contestant)readIn;
				contestantList.add(contestant);
				
			}
			contestants.close();
		} catch (Exception e) {
			//System.out.println(e.getMessage());
		}
		
		//code to read data in for judges
		try {
			ObjectInputStream judges = new ObjectInputStream(new FileInputStream("Output/judges.dat"));
			Object readIn;
			while ((readIn = judges.readObject()) != null) {
				Judge judge = (Judge)readIn;
				judgeList.add(judge);
				
			}
			judges.close();
		} catch (Exception e) {
			//System.out.println(e.getMessage());
		}
	}
	
	public static void saveData() {
		//code to write contestants to file
		try {
			ObjectOutputStream contestants = new ObjectOutputStream(new FileOutputStream("Output/contestant.dat"));
			for (Contestant c :contestantList) {
				contestants.writeObject(c);
			}
			contestants.flush();
			contestants.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//code to write judges to file
		try {
			ObjectOutputStream judges = new ObjectOutputStream(new FileOutputStream("Output/judges.dat"));
			for (Judge j :judgeList) {
				judges.writeObject(j);
			}
			judges.flush();
			judges.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	
	
}
